<template>
	<el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
		<i class="iconfont iconfanhui1" @click="backLoin"></i>
		<el-form-item label="用户名" prop="username">
			<el-input v-model="ruleForm.username" autocomplete="off"></el-input>
		</el-form-item>
		<el-form-item label="密码" prop="pass">
			<el-input type="password" v-model="ruleForm.pass" autocomplete="off"></el-input>
		</el-form-item>
		<el-form-item label="确认密码" prop="checkPass">
			<el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
		</el-form-item>
		<el-form-item label="邮箱" prop="email">
			<el-input v-model="ruleForm.email" autocomplete="off"></el-input>
		</el-form-item>
		<div class="button">
			<el-button type="primary" @click="submit('ruleForm')">提交</el-button>
			<el-button @click="resetForm('ruleForm')">重置</el-button>
		</div>
	</el-form>
</template>

<script>
	export default {
		data() {
			var validateUser = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('用户名不能为空'));
				}
				let patter = /^[a-zA-Z0-9_-]{4,16}$/; //用户名云端验证
				setTimeout(() => {
					if (!patter.test(value)) {
						callback(new Error(' 4到16位（字母，数字，下划线, 横杠)'));
					} else {
						callback();
					}

				}, 1000);
			};
			var checkEmail = (rule, value, callback) => {
				if (!value) {
					return callback(new Error('邮箱不为空！')); //邮箱验证
				}
				setTimeout(() => {
					let patter =
						/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
					if (!patter.test(value)) {
						callback(new Error('请输入正确的邮箱格式'));
					} else {
						callback();
					}
				}, 1000);
			};
			var validatePass = (rule, value, callback) => {
				let patter = /^(\w){6,10}$/;
				if (value === '') {
					callback(new Error('请输入密码'));
				} else if (!patter.test(value)) {
					callback(new Error('请输入6-10位字母、数字、下划线'));
				} else {
					if (this.ruleForm.checkPass !== '') {
						this.$refs.ruleForm.validateField('checkPass');
					}
					callback();
				}
			};
			var validatePass2 = (rule, value, callback) => {
				if (value === '') {
					callback(new Error('请再次输入密码'));
				} else if (value !== this.ruleForm.pass) {
					callback(new Error('两次输入密码不一致!'));
				} else {
					callback();
				}
			};
			return {
				ruleForm: {
					pass: '',
					email: '',
					username: ''
				},
				rules: {
					username: [{
						validator: validateUser,
						trigger: 'blur'
					}],
					pass: [{
						validator: validatePass,
						trigger: 'blur'
					}],
					checkPass: [{
						validator: validatePass2,
						trigger: 'blur'
					}],
					email: [{
						validator: checkEmail,
						trigger: 'blur'
					}]
				}
			};
		},
		methods: {
			backLoin() {
				this.$emit('registerSuccess')
			},
			submit(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						let data = this.$userClass.Cregister(this.ruleForm.username, this.ruleForm.pass, this.ruleForm.email)
						this.$http.post('/auth/register', data).then(res => {
							if (res.status == 200) {
								this.$message({
									type: 'success',
									message: '注册成功！'
								})
								this.$emit('registerSuccess')
							}
						}).catch(err => {
							if(err.response.status == 400){
								this.$message.error('注册失败，账号重复！')
							}
							else{
								this.$message.error('注册失败，网络错误！')
							}
						})
					} else {
						return false;
					}
				});
			},

			resetForm(formName) {
				this.$refs[formName].resetFields();
			}
		}
	}
</script>

<style scoped="scoped">
	.button{
		width: 100%;
		position: absolute;
		bottom: 20px;
		left: 50%;
		transform: translateX(-50%);
		display: flex;
		justify-content: center;
	}
	.el-input{
		width: 90%;
	}
	.iconfanhui1 {
		position: absolute;
		top: 10px;
		left: 20px;
		font-size: 20px;
		color: #999999;
		cursor: pointer;
	}

	.el-form-item {
		margin-bottom: 7%;
		margin-top: 0;
		margin-left: -28px;
		width: 445px;
	}
</style>
